package com.practicaljava.lesson11;

import java.util.Random;

class TicTacToePlayer{
    private PlayStrategy strategy;
    private static String[] positions={"","","","","","","","",""};
    
    public void setStrategy(PlayStrategy playStrategy){
        strategy = playStrategy;
    }
    
    public void setPosition(int i, String x){
        positions[i] = x;
    }
    
    public int move(){
    return strategy.computerMove(positions);
    }
    
    static boolean gameOver(){
    for(String p:positions){
        if (p=="") return false;
    }
    return true;
    }
    
    static boolean gameOver(String[] positions){
    for(String p:positions){
        if (p=="") return false;
    }
    return true;
    }
    
    public void reset(){
        for(String p: positions){
            p = "";
        }
    }
}

interface PlayStrategy{
    public int computerMove(String[] positions);
    //returns the number of the button to move to, 9 if there are no more empty positions
}

class RandomMove implements PlayStrategy{
    
    public int computerMove(String[] positions) {
        Random r = new Random();
        int i;
//        if (!TicTacToePlayer.gameOver()){
//        if (!gameOver(positions)){
        if (!TicTacToePlayer.gameOver(positions)){
            do{
                i = r.nextInt(9);
            //    System.out.println(i);
            }
            while ((positions[i]=="O")||(positions[i]=="X"));
            return i;
        }
        else
            return 9;
    }
    
    boolean gameOver(String[] positions){
    for(String p:positions){
        if (p=="") return false;
    }
    return true;
    }
        
    }